package indi.kider.server.bill.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.bill.*;
import indi.kider.common.model.employee.Employee;
import indi.kider.server.bill.service.BalanceTaskService;
import indi.kider.server.bill.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;

/**
 * Created by shixj on 2018/5/9.
 */
@RestController
@RequestMapping("task")
public class TaskController {
    @Autowired
    private BalanceTaskService taskService;
    @Autowired
    private AuthcService authcService;
    /**
     * 内部服务 不显示在api中
     * 必须提供
     *
     * @param taskRegist
     * @return
     */
    @PostMapping("")
    public CommonResponse regist(@RequestBody TaskRegist taskRegist) {
        try {
            taskService.regist(taskRegist);
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /*
     *//**
     * * @api {PUT} task/time
     * * @apiGroup balance-bill
     * * @apiDescription 更新结算任务结算时间（内部服务，起租业务专用） 在起租时可以先不录入结算日期
     * * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/time
     * {"billAttribution":1,"balanceDate":"2018-03-22"}
     * * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":{"id":1,"billAttribution":1,"balanceDate":"2018-03-22","status":null,"waterReading":null,"waterPrice":null,"waterReadingId":null,"waterReadingTime":null,"powerReading":null,"powerPrice":null,"powerReadingId":null,"powerReadingTime":null,"errorMsg":null,"powerBillStatus":null,"waterBillStatus":null}}
     *//*
    @PutMapping("/time")
    public CommonResponse<BalanceTask> updateBalanceTime(@RequestBody BalanceTask balanceTask) {
        return CommonResponse.buildUpdateSuccessResponse(taskService.update(balanceTask));
    }

    *//**
     **  * @api {PUT} task/initalReading
     * * @apiGroup balance-bill
     * * @apiDescription 设置水电初始读数（内部服务，起租业务专用） 在起租时可以先不录入水电初始数据
     * * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/initalReading
     * {"billAttribution":1,"waterReading":"2.00","waterPrice":0.5,powerReading":"12.05","powerPrice":2.1}
     * * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":{"id":1,"billAttribution":1,"balanceDate":"2018-03-22","status":null,"waterReading":2.00,"waterPrice":0.5,"waterReadingId":null,"waterReadingTime":null,"powerReading":12.05,"powerPrice":2.1,"powerReadingId":null,"powerReadingTime":null,"errorMsg":null,"powerBillStatus":null,"waterBillStatus":null}}
     *//*
    @Deprecated
    @PutMapping("/initalReading")
    public CommonResponse<BalanceTask> setInitalReading(@RequestBody BalanceTask balanceTask) {
        return CommonResponse.buildUpdateSuccessResponse(taskService.setInitalReading(balanceTask));
    }*/

    /**
     * @api {POST} task/all
     * @apiGroup balance-bill
     * @apiDescription 查询结算任务（不分页） type:同账单类型 status:1:正常2:历史3:异常
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/all
     * {"communityId":-1,"housesId":-1,"type":-1,"status":3,"expression":""}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":238,"billAttribution":87,"balanceDate":"2018-05-26","balanceCycle":1,"status":3,"price":3.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":null,"type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":382,"billAttribution":87,"balanceDate":"2018-06-27","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"水结算异常:【null】","type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":384,"billAttribution":87,"balanceDate":"2018-06-27","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"电结算异常:【null】","type":2,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":422,"billAttribution":91,"balanceDate":"2018-06-28","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"水结算异常:【null】","type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"日日鲁嘻嘻","community":"日日公寓","communityId":15,"expression":"","housesId":"74","tenantName":"1","tenantTel":"1"},{"id":424,"billAttribution":91,"balanceDate":"2018-06-28","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"电结算异常:【null】","type":2,"houseNumb":"WN_0000","building":null,"unit":null,"address":"日日鲁嘻嘻","community":"日日公寓","communityId":15,"expression":"","housesId":"74","tenantName":"1","tenantTel":"1"}]}
     */
    @PostMapping("all")
    public CommonResponse<List<RentTask>> query(@RequestBody RentTask rentTask) {
        return CommonResponse.buildDefaultSuccessResponse(taskService.query(rentTask));
    }

    /**
     * @api {POST} task/list
     * @apiGroup balance-bill
     * @apiDescription 查询结算任务（分页） type:同账单类型 status:1:正常2:历史3:异常
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/list
     * {"num":"0","size":"8","query":{"communityId":-1,"housesId":-1,"type":-1,"status":3,"expression":""}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":{"rows":[{"id":238,"billAttribution":87,"balanceDate":"2018-05-26","balanceCycle":1,"status":3,"price":3.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":null,"type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":382,"billAttribution":87,"balanceDate":"2018-06-27","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"水结算异常:【null】","type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":384,"billAttribution":87,"balanceDate":"2018-06-27","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"电结算异常:【null】","type":2,"houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":3,"expression":"","housesId":"69","tenantName":"1","tenantTel":"1"},{"id":422,"billAttribution":91,"balanceDate":"2018-06-28","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"水结算异常:【null】","type":1,"houseNumb":"WN_0000","building":null,"unit":null,"address":"日日鲁嘻嘻","community":"日日公寓","communityId":15,"expression":"","housesId":"74","tenantName":"1","tenantTel":"1"},{"id":424,"billAttribution":91,"balanceDate":"2018-06-28","balanceCycle":1,"status":3,"price":10.0,"reading":null,"readingId":null,"readingTime":null,"errorMsg":"电结算异常:【null】","type":2,"houseNumb":"WN_0000","building":null,"unit":null,"address":"日日鲁嘻嘻","community":"日日公寓","communityId":15,"expression":"","housesId":"74","tenantName":"1","tenantTel":"1"}],"total":5,"size":1}}
     */
    @PostMapping("list")
    public CommonResponse<PageResponse<List<RentTask>>> getRentTask(@RequestBody PageRequest<RentTask> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if(employee!=null&&!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        PageUtil.start(request);
        return CommonResponse.buildDefaultSuccessResponse(PageUtil.getPageResponse(taskService.getRentTask(request.getQuery())));
    }

    /**
     * @api {PUT} task/bill/all
     * @apiGroup balance-bill
     * @apiDescription 退租结算出单
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/bill/all
     * {"rentId":1,"waterReading":1.2,"powerReading":1.3}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":""}
     */
    @PutMapping("bill/all")
    public CommonResponse<Boolean> leaseBalance(@RequestBody BillLeaseCancel billLeaseCancel) {
        if(taskService.leaseBalance(billLeaseCancel)){
            return CommonResponse.buildBadRequestResponse("数据异常，详情请查看异常结算任务！");
        }else return CommonResponse.buildDefaultSuccessResponse("");
    }
    /**
     * @api {PUT} task/error
     * @apiGroup balance-bill
     * @apiDescription 手动抄表处理异常任务  type:1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/task/error
     * {"id":1,"reading":1.2,"type":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":{"id":1,"billType":1,"payState":1,"billTime":"2018-05-10 08:58:53","payTime":"2018-05-10 08:00:00","billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":"11111","state":null,"payType":null}}
     */
    @PutMapping("error")
    public CommonResponse<Bill> resovleErrorTask(@RequestBody BalanceTask task) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(taskService.resovleErrorTask(task));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
